<#-- 充值产品特价设置模板  -->
<#include "../template/template_listPage.ftl">
<@header title="充值产品特价设置">
<style type="text/css">
    .table {
        padding: 1px;
        text-align: center;
    }

    .inputEdit {
        width: 60px;
    }
</style>
<script type="text/javascript">
    function query() {
        $('#listForm').submit();
    }

    function validator() {
        var $validateErrorContainer = $("#validateErrorContainer");
        var $validateErrorLabelContainer = $("#validateErrorContainer ul");
        var $validateForm = $("#validateForm");
        var $tab = $("#tab");

        // Tab效果
        $tab.tabs(".tabContent", {
            tabs:"input"
        });

        // 表单验证
        $validateForm.validate({
            errorContainer:$validateErrorContainer,
            errorLabelContainer:$validateErrorLabelContainer,
            wrapper:"li",
            errorClass:"validateError",
            ignoreTitle:true,
            rules:{
                "category.name":{
                    required:true
                }
            },
            messages:{
                "category.name":{
                    required:"请填写商品类别名称"
                }
            },
            submitHandler:function (form) {
                $(form).find(":submit").attr("disabled", true);
                form.submit();
            }
        });
		<#if actionErrors??&&(actionErrors?size > 0)>
			$.dialog({type: "warn", content: "<#list errorMessages as list>${list}<br></#list>",
			 modal: true, autoCloseTime: 3000});
		</#if>
    }
    /**
     * 批量设置
     */
    function batchSet() {
        var checkbox = $("input[type='checkbox'][id^='CB_']:checked");
        var priceTemplateCode = '<#if pt??>${pt.id!}</#if>';
        if (checkbox.length > 0) {
            for (var i = 0; i < checkbox.length; i++) {
                var webId = checkbox[i].id;
                webId=webId.split("_")[1];
                var id = checkbox[i].value;
                var channelAgentPrice = $('#channelAgentPrice_' + webId).val();
                var doorPrice = $('#doorPrice_' + webId).val();
                var doorAddPrice = $('#doorAddPrice_' + webId).val();
                var systemProductId = $('#systemProductId_' + webId).val();
                //alert(id + ',' + channelAgentPrice + ',' + doorPrice + ',' + doorAddPrice);
                if (channelAgentPrice == '') {
                    var name = $('#name_' + webId).text();
                    alert(name + ",请设置渠道价!");
                    return;
                }
                if ((channelAgentPrice != '' && isNaN(channelAgentPrice)) ||
                        (doorPrice != '' && isNaN(doorPrice)) ||
                        (doorAddPrice != '' && isNaN(doorAddPrice))) {
                    var name = $('#name_' + webId).text();
                    alert(name + "价格设置有误!");
                    return;
                }
                if(parseFloat(channelAgentPrice)<=0||parseFloat(doorPrice)<=0||parseFloat(doorAddPrice)<0){
                    alert("价格必须大于零!");
                    return;
                }
                if (parseFloat(channelAgentPrice).toFixed(3) > parseFloat(doorPrice).toFixed(3)) {
                    alert("渠道价必须小于等于门店价!");
                    return;
                }
            }

            for (var i = 0; i < checkbox.length; i++) {
                var webId = checkbox[i].id;
                webId=webId.split("_")[1];
                var id = checkbox[i].value;
                var channelAgentPrice = $('#channelAgentPrice_' + webId).val();
                var doorPrice = $('#doorPrice_' + webId).val();
                var doorAddPrice = $('#doorAddPrice_' + webId).val();
                var systemProductId = $('#systemProductId_' + webId).val();
                //alert(id + ',' + channelAgentPrice + ',' + doorPrice + ',' + doorAddPrice);

                var sendData = 'id=' + id + "&systemProductId=" + systemProductId + "&channelAgentPrice=" + channelAgentPrice +
                        "&doorPrice=" + doorPrice + "&doorAddPrice=" + doorAddPrice + "&templateCode=" + priceTemplateCode;

                $.ajax({
                    type:"POST",
                    url:'${web_base}/recharge/price_template_special!doAddOrEdit.htm',
                    data:sendData,
                    success:function (msg) {
                        var data = eval('(' + msg + ')');
                        if (data.result == "success") {
                            var id = data.id;
                            if (id != null && id != '') {
                                $('#' + webId).val(id);
                            }
                            $.dialog({type: "warn", content: "操作成功!",modal: true, autoCloseTime: 3000});
                        } else if (data.result == "error") {
                            var name = $('#name_' + webId).text();
                            $.dialog({type: "warn", content: name + "操作失败!",modal: true, autoCloseTime: 3000});
                        }
                    }
                });
            }
        } else {
            alert("本次未有设置产品");
        }
        return;
    <#--
    $("input[type='checkbox']:checked").each(function () {
        var webId = $(this).attr("id");
        var id = $(this).val();
        var channelAgentPrice = $('#channelAgentPrice_' + webId).val();
        var doorPrice = $('#doorPrice_' + webId).val();
        var doorAddPrice = $('#doorAddPrice_' + webId).val();
        alert(id + ',' + channelAgentPrice + ',' + doorPrice + ',' + doorAddPrice);

        var sendData = 'id=' + id + "&channelAgentPrice=" + channelAgentPrice + "&doorPrice=" + doorPrice + "&doorAddPrice=" + doorAddPrice;

        $.ajax({
            type:"POST",
            url:'${web_base}/recharge/currency_price_manage!doEdit.htm',
            data:sendData,
            success:function (msg) {
                var data = eval('(' + msg + ')');
                alert(msg);

            }
        });
    });
    -->
    }
    function selectAll() {
        if ($('#selectCB').attr("checked") == 'checked') {
            $('input[type="checkbox"]').each(function () {
                $(this).attr("checked", true);
            });
        } else {
            $('input[type="checkbox"]').each(function () {
                $(this).attr("checked", false);
            });
        }
    }
    $(document).ready(function () {
        $('#productType').val('${productType!}');
//        validator();
    });
</script>
</@header>
<body class="list">
<div class="body">
    <div class="listBar">
        <h1><span class="icon">&nbsp;</span>充值产品特价设置&nbsp;<span class="pageInfo">总记录数:<#if page??>${page.rowCount}</#if>
            (共<#if page??>${page.pageCount}</#if>页)</span></h1>
    </div>
    <div style="display: none;" id="validateErrorContainer" class="validateErrorContainer">
        <div class="validateErrorTitle">以下信息填写有误,请重新填写</div>
        <ul></ul>
    </div>
<@pageList page=(page!) id="listForm" action="" query="ptsQuery" isEmpty=true>
    <div class="operateBar">
        <label>当前模板:</label>
        <span style="color: red;font-size: 14px;"><#if pt??>${pt.name!}</#if></span>
        <input type="hidden" name="templateCode" value="${templateCode!}">
        <label style="display: none;">产品类型</label>
        <select name="productType" id="productType" style="display: none;">
            <option value="1">直冲产品</option>
            <option value="0">卡密产品</option>
        </select>
        <button type="button" onclick="window.location.href='${web_base}/recharge/price_template!list.htm'">返回</button>
        <button type="button" onclick="batchSet();">批量设置</button>
        <input type="checkbox" id="selectCB" onclick="selectAll();"><label for="selectCB"
                                                                            style="padding-left: 2px;">全选</label>
    </div>
    <table class="listTable">
        <tr>
            <th><span class="sort">序号</span></th>
            <th><span class="sort">产品</span></th>
            <th><span class="sort">面值(元)</span></th>
            <th><span class="sort">渠道价(元)</span></th>
            <th><span class="sort">门店价(元)</span></th>
            <th><span class="sort">门店加扣(元)</span></th>
            <th><span class="sort">选择</span></th>
        </tr>
        <#if  page??&&page.list??&&(page.list?size gt 0) >
            <#list page.list as obj>
                <#assign  id=obj_index+page.pageSize*(page.currentPage-1)+1/>
                <tr>
                    <td>${id!}</td>
                    <td><span id="name_${id!}">${obj.productName!}</span></td>
                    <td><#--<#if obj.faceValue??>${(obj.faceValue*1.0/1000)?string("0.000")}</#if>-->1.00</td>
                    <td><input class="inputEdit" type="text" name="" id="channelAgentPrice_${id!}"
                               value="${money((obj.channelAgentPrice)!)}">
                    </td>
                    <td><input class="inputEdit" type="text" name="" id="doorPrice_${id!}"
                               value="${money((obj.doorPrice)!)}"></td>
                    <td><input class="inputEdit" type="text" name="" id="doorAddPrice_${id!}"
                               value="${money((obj.doorAddPrice)!)}">
                    </td>
                    <td><input type="checkbox" name="" value="${obj.id!}" id="CB_${id!}">
                        <input type="hidden" value="${obj.systemProductId!}" id="systemProductId_${id!}">
                    </td>
                </tr>
            </#list>
        </#if>
    </table>
</@pageList>
    <div style="margin-left: 5px;margin-top: 50px;position: absolute;"><p style="color: red;">注：<br>
        1.门店进价不填或写0则为不设置门店进价，代理商进价为必填项。<br>
        2.门店加扣为每笔订单的加扣金额。<br>
        3.在设置时价格必须符合'进价' 小于或等于 '渠道价' 小于或等于 '门店价' <br>
        4.设置完成后，需要在"下级用户管理》代理商管理"里挂上设置的模板才有效
    </p>
    </div>
</div>
</body>
</html>